package com.zhouchb.stream;

import java.io.*;

/**
 * @Author Zhouchb
 * @Create 2021-11-2021/11/1 :20:21
 * @Description
 */
public class ObjectInputOutputStream {
    public static void main(String[] args) {
        out();
        in();
    }
    //序列化
    public static void out(){
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("IO/object.dat"));
            oos.writeObject(new String("周纯彬你好帅"));
            oos.flush();
            oos.writeObject(new Person("林动",23));
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos!=null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    //反序列化
    public static void in(){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("IO/object.dat"));
            String o = (String) ois.readObject();
            Person person = (Person) ois.readObject();
            System.out.println(o);
            System.out.println(person);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois!=null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
